package com.th.supcom.pds.config.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridViewConfig;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.th.supcom.pds.config.client.common.PdsConfigConstant;
import com.th.supcom.pds.config.client.common.PdsConfigEnableEnum;
import com.th.supcom.pds.config.client.common.PdsConfigExistEnum;
import com.th.supcom.pds.config.client.common.PdsConfigRangeEnum;
import com.th.supcom.pds.config.client.model.PhamDispensaryConfigModel;

/**
 * PhamDispensaryConfig主面板.
 * 
 * 药房属性.
 */
public class PhamDispensaryConfigMainPanel extends ContentPanel
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private final PhamDispensaryConfigAction m_action = new PhamDispensaryConfigAction ();
    /** Grid数据 */
    private Grid <PhamDispensaryConfigModel> m_dispensaryGrid;
    /** 操作按钮 */
    private Button m_addButton;
    private Button m_editButton;

    private final ListStore <PhamDispensaryConfigModel> m_listStore = new ListStore <PhamDispensaryConfigModel> ();

    private Button m_refreshButton;

    private Button m_removeButton;

    public PhamDispensaryConfigMainPanel ()
    {
        this.initUI ();
        m_action.registerListeners (this);
    }

    /**
     * 添加不需渲染的列
     * 
     * @param configs
     */
    private void addNotRenderColumn (final List <ColumnConfig> configs)
    {
        ColumnConfig column;
        column = new ColumnConfig ("phamDispensaryName", "所属药房", 100);
        column.setAlignment (HorizontalAlignment.LEFT);
        configs.add (column);

        column = new ColumnConfig ("phamName", "摆药类型", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("shiftName", "班次" + PdsConfigConstant.DEFALUT_HTML_WRAP + "类型", 38);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("startTime", "起始时间", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("endTime", "结束时间", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("openFlag", "是否" + PdsConfigConstant.DEFALUT_HTML_WRAP + "启用", 45);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                final Button btn = new Button ();
                final Integer updateValue;
                btn.setWidth (35);
                btn.setAutoHeight (true);
                if (configModel.getOpenFlag () == PdsConfigEnableEnum.Enable.getCode ())
                {
                    btn.setText ("禁用");
                    updateValue = PdsConfigEnableEnum.Disable.getCode ();
                }
                else
                {
                    btn.setText ("启用");
                    updateValue = PdsConfigEnableEnum.Enable.getCode ();
                }
                btn.addSelectionListener (new SelectionListener <ButtonEvent> ()
                {

                    @Override
                    public void componentSelected (final ButtonEvent ce)
                    {
                        enableBtnAction (configModel, updateValue);
                    }

                });
                return btn;

            }
        });
        configs.add (column);

        column = new ColumnConfig ("searchRange", "查询范围", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigRangeEnum.getConfigSearchRangeNameByValue (configModel.getSearchRange ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("prescDateBefore", "查询处方" + PdsConfigConstant.DEFALUT_HTML_WRAP + "日期设置", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        column = new ColumnConfig ("deliveryTypeName", "出库类型", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);
    }

    /**
     * 生成表格列字段
     * 
     * @return
     */
    private ColumnModel createColumnModel ()
    {
        final List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();

        ColumnConfig column = new ColumnConfig ();
        PhamConfigWidgetUtils.generateGridSeq (configs, column);

        addNotRenderColumn (configs);

        column = new ColumnConfig ("dispensePrintFlag", "是否打印" + PdsConfigConstant.DEFALUT_HTML_WRAP + "摆药单", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigEnableEnum.getConfigEnableNameByValue (configModel.getDispensePrintFlag ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("infusionConfig", "静脉输液" + PdsConfigConstant.DEFALUT_HTML_WRAP + "配置", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigExistEnum.getConfigExistNameByValue (configModel.getInfusionConfig ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("infusionOpen", "是否" + PdsConfigConstant.DEFALUT_HTML_WRAP + "静配", 38);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigEnableEnum.getConfigEnableNameByValue (configModel.getInfusionOpen ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("decoteConnFlag", "是否连接" + PdsConfigConstant.DEFALUT_HTML_WRAP + "煎药机", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigEnableEnum.getConfigEnableNameByValue (configModel.getDispensePrintFlag ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("queueConfig", "排队系统" + PdsConfigConstant.DEFALUT_HTML_WRAP + "配置", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigExistEnum.getConfigExistNameByValue (configModel.getQueueConfig ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("queueEnable", "是否开放" + PdsConfigConstant.DEFALUT_HTML_WRAP + "排队系统", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (final ModelData model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex, final ListStore <ModelData> store,
                                  final Grid <ModelData> grid)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                return PdsConfigEnableEnum.getConfigEnableNameByValue (configModel.getQueueEnable ());
            }
        });
        configs.add (column);

        column = new ColumnConfig ("queneName", "排队系统" + PdsConfigConstant.DEFALUT_HTML_WRAP + "提示类型", 60);
        column.setAlignment (HorizontalAlignment.CENTER);
        configs.add (column);

        final ColumnModel cm = new ColumnModel (configs);
        return cm;
    }

    private void enableBtnAction (final PhamDispensaryConfigModel configModel, final Integer updateValue)
    {
        final PhamDispensaryConfigModel updateModel = new PhamDispensaryConfigModel ();
        updateModel.setPhamDispensaryConfigId (configModel.getPhamDispensaryConfigId ());
        updateModel.setPhamDispensaryCode (configModel.getPhamDispensaryCode ());
        updateModel.setOpenFlag (updateValue);
        m_action.enableBtnListener (updateModel, this);
    }

    public Grid <PhamDispensaryConfigModel> getDispensaryGrid ()
    {

        return m_dispensaryGrid;
    }

    public Button getEditButton ()
    {

        return m_editButton;
    }

    public ListStore <PhamDispensaryConfigModel> getListStore ()
    {

        return m_listStore;
    }

    public Button getRefreshButton ()
    {

        return m_refreshButton;
    }

    public Button getRemoveButton ()
    {

        return m_removeButton;
    }
    public Button getAddButton ()
    {

        return m_addButton;
    }
    /**
     * 初始化UI控件.
     * 
     */
    private void initUI ()
    {
        this.setLayout (new FitLayout ());
        this.setHeaderVisible (false);
        this.setBodyBorder (false);
        this.setBorders (false);
        this.setStyleAttribute ("margin", "1");
        m_addButton = new Button ();
        m_editButton = new Button ();
        m_removeButton = new Button ();
        m_refreshButton = new Button ();
        final ToolBar toolBar = PhamConfigWidgetUtils.generateToolBar (m_addButton,m_editButton, m_removeButton,
                                                                       m_refreshButton);
        m_dispensaryGrid = new Grid <PhamDispensaryConfigModel> (m_listStore, createColumnModel ());
        m_dispensaryGrid.setStateful (true);
        m_dispensaryGrid.setStyleAttribute ("borderTop", "none");
        m_dispensaryGrid.setStripeRows (true);
        m_dispensaryGrid.getView ().setForceFit (true);
        m_dispensaryGrid.getView ().setEmptyText (PdsConfigConstant.GRID_NOT_RECORD);
        m_dispensaryGrid.setBorders (false);
        m_dispensaryGrid.setLoadMask (true);
        m_dispensaryGrid.setColumnLines (true);
        m_dispensaryGrid.getSelectionModel ().setSelectionMode (SelectionMode.SINGLE);
        m_dispensaryGrid.getView ().setViewConfig (new GridViewConfig ()
        {
            @Override
            public String getRowStyle (final ModelData model, final int rowIndex, final ListStore <ModelData> ds)
            {
                final PhamDispensaryConfigModel configModel = (PhamDispensaryConfigModel) model;
                if (configModel != null && configModel.getOpenFlag () != null
                    && PdsConfigEnableEnum.Disable.getCode () == configModel.getOpenFlag ())
                {
                    return PdsConfigConstant.GRAY;
                }
                return null;
            }
        });
        this.add (m_dispensaryGrid);
        this.setTopComponent (toolBar);
    }

    @Override
    protected void onRender (final Element parent, final int index)
    {
        super.onRender (parent, index);
    }

    
}